package com.speily.web.filter;

import com.speily.web.http.Request;
import com.speily.web.http.Response;
import com.speily.web.servlet.HttpServlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: SPL
 * @Date: 2019-07-30 17:59
 * @Description: - 过滤链
 */
public class FilterChain {

    private HttpServlet servlet;

    private int pos = 0;

    private List<Filter> filterList = new ArrayList<>();

    public void doFilter(Request request, Response response) throws IOException {

        if (pos < filterList.size()) {
            filterList.get(pos++).doFilter(request, response, this);
        } else {
            getServlet().service(request, response);
        }
    }

    public boolean addFilter(Filter filter) {
        return filterList.add(filter);
    }

    public void setServlet(HttpServlet servlet) {
        this.servlet = servlet;
    }

    public HttpServlet getServlet() {
        return servlet;
    }
}
